package com.qf.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebFilter({"/admin/goodsManager.jsp", "/admin/index.jsp", "/admin/sysLogin.jsp","/admin/sys_user.jsp"})
public class SysLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig)  {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        String uri = req.getRequestURI();

        if (uri.contains("/admin/sysLogin.jsp")) {

            filterChain.doFilter(req, resp);
            return;
        }

        HttpSession session = req.getSession();
        // 获取用户在登录成功之后设置的用户信息
        Object object = session.getAttribute("sys_user");
        if (null != object) {
            filterChain.doFilter(req, resp);
            return;
        }


        resp.sendRedirect("/admin/sysLogin.jsp");



    }


    @Override
    public void destroy() {

    }
}
